package com.atguigu.realtime.publisher.traffic.controller;

import com.alibaba.druid.support.json.JSONParser;
import com.atguigu.realtime.publisher.traffic.bean.TrafficKeyword;
import com.atguigu.realtime.publisher.traffic.bean.TrafficSession;
import com.atguigu.realtime.publisher.traffic.bean.TrafficUvCt;
import com.atguigu.realtime.publisher.traffic.service.TrafficChannelStatsService;
import com.atguigu.realtime.publisher.util.GetTimeUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/traffic")
public class TrafficChannelStatsController {

    @Autowired
    TrafficChannelStatsService trafficChannelStatsService;

    @RequestMapping("/uvCt")
    public String getUvCt(@RequestParam(value = "date", defaultValue = "1", required = false) String date){
        if ("1".equals(date)){
            // 需要换成今日
            date = GetTimeUtil.getToday();
        }

        List<TrafficUvCt> trafficUvCtList = trafficChannelStatsService.getUvCt(date);

        if (trafficUvCtList==null) return "";

        String categories = trafficUvCtList.stream().map(TrafficUvCt::getCh).collect(Collectors.joining("\",\"", "\"", "\""));
        String datas = trafficUvCtList.stream().map(TrafficUvCt::getUvCt).map(
                new Function<Integer, String>() {
                    @Override
                    public String apply(Integer integer) {
                        return integer + "";
                    }
                }
        ).collect(Collectors.joining(","));


        return "{\n" +
                "\"status\": 0,\n" +
                "\"msg\": \"\",\n" +
                "\"data\": {\n" +
                "\"categories\": ["+
                categories
                +"],\n" +
                "\"series\": [\n" +
                "{\n" +
                "\"name\": \"渠道独立访问数\",\n" +
                "\"data\": ["+
                datas
                +"]\n" +
                "}\n" +
                "]\n" +
                "}\n" +
                "}";
    }


    @RequestMapping("/Session")
    public String getSessionInfo(@RequestParam(value = "date", defaultValue = "1", required = false) String date){
        if ("1".equals(date)){
            // 需要换成今日
            date = GetTimeUtil.getToday();
        }

        List<TrafficSession> sessionInfoList = trafficChannelStatsService.getSessionInfo(date);

        if (sessionInfoList == null) return "";


        String categories = sessionInfoList.stream().map(TrafficSession::getCh).collect(Collectors.joining("\",\"", "\"", "\""));
        String dataOfSvCt = sessionInfoList.stream().map(TrafficSession::getSvCt).map(
                integer -> integer + ""
        ).collect(Collectors.joining(","));
        String dataOfAvgCt = sessionInfoList.stream().map(TrafficSession::getAvgCt).map(
                aDouble -> aDouble + ""
        ).collect(Collectors.joining(","));
        String dataOfAvgTime = sessionInfoList.stream().map(TrafficSession::getAvgTime).map(
                aDouble -> aDouble + ""
        ).collect(Collectors.joining(","));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" +
                "      "+categories+"\n" +
                "    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"会话总数\",\n" +
                "        \"data\": [\n" +
                "          "+dataOfSvCt+"\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"会话平均浏览页面数\",\n" +
                "        \"data\": [\n" +
                "          "+dataOfAvgCt+"\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"会话平均停留时长\",\n" +
                "        \"data\": [\n" +
                "          "+dataOfAvgTime+"\n" +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";


    }


    @RequestMapping("/keyword")
    public String getKC(@RequestParam(value = "date", defaultValue = "1", required = false) String date){
        if ("1".equals(date)){
            date = GetTimeUtil.getToday();
        }

        List<TrafficKeyword> keywordCountList = trafficChannelStatsService.getKeywordCount(date);

        if (keywordCountList == null) return "";

        /*ObjectMapper objectMapper = new ObjectMapper();

        String json = "";
        try {
            json = objectMapper.writeValueAsString(keywordCountList);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }*/
        /*String data = keywordCountList.stream().map(bean -> " {\"name\": \"" + bean.getKeyword() + "\",\"value\": " + bean.getKeywordCount() + "}")
                .collect(Collectors.joining(","));*/

        String data = keywordCountList.stream().map(bean -> "{\"name\":\"" + bean.getKeyword() + "\",\"value\":" + bean.getKeywordCount() + "}").collect(Collectors.joining(","));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": [\n" +
                "    "+data+"\n" +
                "  ]\n" +
                "}";

    }
}
